package com.hishop.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hishop.common.utils.PageUtils;
import com.hishop.common.utils.Query;
import com.hishop.product.dao.AttrAttrgroupRelationDao;
import com.hishop.product.dao.AttrDao;
import com.hishop.product.dao.AttrGroupDao;
import com.hishop.product.dao.CategoryDao;
import com.hishop.product.entity.AttrEntity;
import com.hishop.product.entity.CategoryEntity;
import com.hishop.product.service.AttrAttrgroupRelationService;
import com.hishop.product.service.AttrService;
import com.hishop.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {
    @Autowired
    private AttrDao attrDao;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Autowired
    AttrGroupDao attrGroupDao;

    @Autowired
    CategoryDao categoryDao;

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catId) {
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<AttrEntity>();
        if (params.containsKey("key")) {
            String key = params.get("key").toString();
            wrapper.and((obj) -> {
                obj.eq("attr_id", key).or().like("attr_name", key);
            });
        }
        IPage<AttrEntity> page = new Page<>();
        if (catId == 0) {
            page = this.page(new Query<AttrEntity>().getPage(params), wrapper);
        } else {
            wrapper.eq("catelog_id", catId);
            page = this.page(new Query<AttrEntity>().getPage(params), wrapper);
        }

        // 遍历AttrEntity 列表
        for (AttrEntity attr : page.getRecords()) {
            Long attrId = attr.getAttrId();// 获取属性id【用来查询分组id】
            attrAttrgroupRelationService.selectGroupName(attr, attrId);

            /**
             * 获取 分类id
             * 拿到分类对象获取分类名字
             * 设置分类名字
             */
            Long catelogId = attr.getCatelogId();
            CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
            if (catelogId != null) {
                attr.setCatelogName(categoryEntity.getName());
            } else {
                attr.setCatelogName("——");
            }
        }
        return new PageUtils(page);

    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catId,Long type) {
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<AttrEntity>().eq("attr_type",type);
        if (params.containsKey("key")) {
            String key = params.get("key").toString();
            wrapper.and((obj) -> {
                obj.eq("attr_id", key).or().like("attr_name", key);
            });
        }
        IPage<AttrEntity> page = new Page<>();
        if (catId == 0) {
            page = this.page(new Query<AttrEntity>().getPage(params), wrapper);
        } else {
            wrapper.eq("catelog_id", catId);
            page = this.page(new Query<AttrEntity>().getPage(params), wrapper);
        }

        // 遍历AttrEntity 列表
        for (AttrEntity attr : page.getRecords()) {
            Long attrId = attr.getAttrId();// 获取属性id【用来查询分组id】
            attrAttrgroupRelationService.selectGroupName(attr, attrId);

            /**
             * 获取 分类id
             * 拿到分类对象获取分类名字
             * 设置分类名字
             */
            Long catelogId = attr.getCatelogId();
            CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
            if (catelogId != null) {
                attr.setCatelogName(categoryEntity.getName());
            } else {
                attr.setCatelogName("——");
            }
        }
        return new PageUtils(page);

    }


    /**
     * 修改 属性和分组的关联信息
     *
     * @param attr
     */
    @Override
    public void updateDetail(AttrEntity attr) {
        // System.out.println( "1111111111111" + attr.toString());
        // 分类service配置一个节点路径的查找
        Long[] path = categoryService.findCatelogPath(attr.getCatelogId());
        attr.setCatelogPath(path);
        // 获取分组名字
        String groupName = attrGroupDao.selectById(attr.getAttrGroupId()).getAttrGroupName();
        attr.setGroupName(groupName);
        // System.out.println("2222222222" + attr.getGroupName());
        // 保证其他数据一致
        this.updateById(attr);

        if (attr.getAttrId() != null) {

            attrAttrgroupRelationService.updateGroupId(attr);
            // System.out.println("333333333333333333333333"+attr.getGroupName());
        }

        // 如果 属性分组关联表 没有关于这一条的数据，那么添加这条数据
        if (attrAttrgroupRelationService.selectGroupIdByAttrId(attr.getAttrId()) == null) {
            // 设置 属性和分组关联数据
            attrAttrgroupRelationService.saveDetail(attr);
        }
    }

    @Override
    public PageUtils queryPage1(Map<String, Object> params, Long attrGroupId) {
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        // 获取原本 分组id就是 attrGroupId 的
        Long[] attrIds = attrAttrgroupRelationService.selectAttrId(attrGroupId);
        for (int i = 0; i < attrIds.length; i++) {
            wrapper.ne("attr_id",attrIds[i]);
        }
        if (params.containsKey("key")) {
            String key = params.get("key").toString();
            wrapper.and((obj) -> {
                obj.eq("attr_id", key).or().like("attr_name", key);
            });
        }

        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public List<Long> selectSearchAttrs(List<Long> attrIds) {
        List<Long> searchAttrIds = this.baseMapper.selectSearchAttrIds(attrIds);

        return searchAttrIds;
    }
}